package com.dhh.sky.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONObject;
import com.dhh.sky.model.SkyDriveAlbum;
import com.dhh.sky.model.SkyDriveAudio;
import com.dhh.sky.model.SkyDriveFile;
import com.dhh.sky.model.SkyDriveFolder;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.model.SkyDrivePhoto;
import com.dhh.sky.model.SkyDriveVideo;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class FileDAO {
	private DataBaseHelper db;
	
	public FileDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public List<SkyDriveObject> getSkyObjectsByParent(String parent){
		List<SkyDriveObject> lst = new ArrayList<SkyDriveObject>();
		Cursor cursor = null;
		try{
			db.openDataBase();
			String whereSQL = "parent='"+parent+"'";
			cursor = db.getReadableDatabase().query(true, "FILES", SkyDriveObject.getColumns(), whereSQL,null, null, null, "type desc,date desc", null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					String json = cursor.getString(4);
					SkyDriveObject obj = null;
					if(json!=null && (obj=initObject(json,cursor.getString(5)))!=null){
						lst.add(obj);						
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public SkyDriveObject getSkyObjectByName(String name){
		SkyDriveObject file = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String whereSQL = "name='"+name+"' and parent='me/skydrive'";
			cursor = db.getReadableDatabase().query(true, "FILES", SkyDriveObject.getColumns(), whereSQL,null, null, null, "type,date desc", null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					String json = cursor.getString(4);
					SkyDriveObject obj = null;
					if(json!=null && (obj=initObject(json,cursor.getString(5)))!=null){
						file = obj;					
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return file;
	} 
	
	public SkyDriveObject getSkyObjectByName(String name,String parent){
		SkyDriveObject file = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String whereSQL = "name='"+name+"' and parent='"+parent+"'";
			cursor = db.getReadableDatabase().query(true, "FILES", SkyDriveObject.getColumns(), whereSQL,null, null, null, "type,date desc", null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					String json = cursor.getString(4);
					SkyDriveObject obj = null;
					if(json!=null && (obj=initObject(json,cursor.getString(5)))!=null){
						file = obj;					
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return file;
	} 
	
	public SkyDriveObject getSkyObjects(String path){
		SkyDriveObject file = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String whereSQL = "path='"+path+"'";
			cursor = db.getReadableDatabase().query(true, "FILES", SkyDriveObject.getColumns(), whereSQL,null, null, null, "type,date desc", null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					String json = cursor.getString(4);
					SkyDriveObject obj = null;
					if(json!=null && (obj=initObject(json,cursor.getString(5)))!=null){
						file = obj;					
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return file;
	}
	
	public SkyDriveObject getSkyLastObjects(String parent){
		SkyDriveObject file = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String whereSQL = "parent='"+parent+"'";
			cursor = db.getReadableDatabase().query(true, "FILES", SkyDriveObject.getColumns(), whereSQL,null, null, null, "date desc", "1");
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					String json = cursor.getString(4);
					SkyDriveObject obj = null;
					if(json!=null && (obj=initObject(json,cursor.getString(5)))!=null){
						file = obj;					
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return file;
	}
	
	public boolean updateName(String path,String name){
		boolean flag = true;
		try{
			db.openDataBase();
			ContentValues values = new ContentValues();
			values.put("name", name);
			db.getWritableDatabase().update("FILES", values, "path=?", new String[]{path});			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return flag;
	}
	
	public SkyDriveObject initObject(String json,String sDate){
		try{
			JSONObject skyDriveObject = new JSONObject(json);
			String type = skyDriveObject.optString("type");
			SkyDriveObject obj = null;
	        if (type.equals(SkyDriveFolder.TYPE)) {
	        	obj = new SkyDriveFolder(skyDriveObject);
	        } else if (type.equals(SkyDriveFile.TYPE)) {
	        	obj = new SkyDriveFile(skyDriveObject);
	        } else if (type.equals(SkyDriveAlbum.TYPE)) {
	        	obj = new SkyDriveAlbum(skyDriveObject);
	        } else if (type.equals(SkyDrivePhoto.TYPE)) {
	        	obj = new SkyDrivePhoto(skyDriveObject);
	        } else if (type.equals(SkyDriveVideo.TYPE)) {
	        	obj = new SkyDriveVideo(skyDriveObject);
	        } else if (type.equals(SkyDriveAudio.TYPE)) {
	        	obj = new SkyDriveAudio(skyDriveObject);
	        } else{
	        	obj = SkyDriveObject.create(skyDriveObject);
	        }
	        
	        if(obj!=null){
	        	obj.setMeta(sDate);
	        	return obj;
	        }
		}catch(Exception e){}
		return null;
	}
	
	public boolean addFile(SkyDriveObject file){
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(file);
		try{
			db.openDataBase();
			long rlt = db.getWritableDatabase().insert("FILES", null, initialValues);
			Log.d("SDA", "DAO add file:"+file.getName()+", rlt:"+rlt);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	public boolean addFiles(List<SkyDriveObject> files){
		boolean isSuc = false;
		
		try{
			db.openDataBase();
			for(SkyDriveObject file : files){
				ContentValues initialValues = createContentValues(file);
				db.getWritableDatabase().insert("FILES", null, initialValues);
			}			
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	public boolean delFile(String path){
		boolean isSuc = false;
		
		try{
			db.openDataBase();
			db.getWritableDatabase().delete("FILES", "path='"+path+"'",null);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return isSuc;
	}
	
	public boolean delFileByParent(String parent){
		boolean isSuc = false;
		
		try{
			db.openDataBase();
			db.getWritableDatabase().delete("FILES", "parent='"+parent+"'",null);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return isSuc;
	}
	
	private ContentValues createContentValues(SkyDriveObject item) {
		ContentValues values = new ContentValues();
		values.put("path", item.getId());
		values.put("name", item.getName());
		values.put("type", item.getType());
		values.put("date", item.getUpdatedTime());
		values.put("parent", item.getParentId());
		values.put("json", item.mObject.toString());
		values.put("meta", new Date().getTime());
		return values;
	}
}
